package cn.ctodb.cms.domain;

import com.fasterxml.jackson.annotation.JsonIgnore;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.springframework.data.elasticsearch.annotations.Document;

import javax.persistence.*;
import javax.validation.constraints.*;
import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;
import java.util.Objects;

/**
 * A CmsChannel.
 */
@Entity
@Table(name = "cms_channel")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
@Document(indexName = "cmschannel")
public class CmsChannel implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @Column(name = "name")
    private String name;

    @NotNull
    @Column(name = "code", nullable = false)
    private String code;

    @Column(name = "sort")
    private Integer sort;

    @Column(name = "tpl_channel")
    private String tplChannel;

    @Column(name = "tpl_content")
    private String tplContent;

    @OneToMany(mappedBy = "cmsChannel")
    @JsonIgnore
    @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
    private Set<CmsTxt> txts = new HashSet<>();

    @OneToMany(mappedBy = "parent")
    @JsonIgnore
    @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
    private Set<CmsChannel> channels = new HashSet<>();

    @ManyToOne
    private CmsModel model;

    @ManyToOne
    private CmsChannel parent;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public CmsChannel name(String name) {
        this.name = name;
        return this;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getCode() {
        return code;
    }

    public CmsChannel code(String code) {
        this.code = code;
        return this;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public Integer getSort() {
        return sort;
    }

    public CmsChannel sort(Integer sort) {
        this.sort = sort;
        return this;
    }

    public void setSort(Integer sort) {
        this.sort = sort;
    }

    public String getTplChannel() {
        return tplChannel;
    }

    public CmsChannel tplChannel(String tplChannel) {
        this.tplChannel = tplChannel;
        return this;
    }

    public void setTplChannel(String tplChannel) {
        this.tplChannel = tplChannel;
    }

    public String getTplContent() {
        return tplContent;
    }

    public CmsChannel tplContent(String tplContent) {
        this.tplContent = tplContent;
        return this;
    }

    public void setTplContent(String tplContent) {
        this.tplContent = tplContent;
    }

    public Set<CmsTxt> getTxts() {
        return txts;
    }

    public CmsChannel txts(Set<CmsTxt> cmsTxts) {
        this.txts = cmsTxts;
        return this;
    }

    public CmsChannel addTxt(CmsTxt cmsTxt) {
        txts.add(cmsTxt);
        cmsTxt.setCmsChannel(this);
        return this;
    }

    public CmsChannel removeTxt(CmsTxt cmsTxt) {
        txts.remove(cmsTxt);
        cmsTxt.setCmsChannel(null);
        return this;
    }

    public void setTxts(Set<CmsTxt> cmsTxts) {
        this.txts = cmsTxts;
    }

    public Set<CmsChannel> getChannels() {
        return channels;
    }

    public CmsChannel channels(Set<CmsChannel> cmsChannels) {
        this.channels = cmsChannels;
        return this;
    }

    public CmsChannel addChannel(CmsChannel cmsChannel) {
        channels.add(cmsChannel);
        cmsChannel.setParent(this);
        return this;
    }

    public CmsChannel removeChannel(CmsChannel cmsChannel) {
        channels.remove(cmsChannel);
        cmsChannel.setParent(null);
        return this;
    }

    public void setChannels(Set<CmsChannel> cmsChannels) {
        this.channels = cmsChannels;
    }

    public CmsModel getModel() {
        return model;
    }

    public CmsChannel model(CmsModel cmsModel) {
        this.model = cmsModel;
        return this;
    }

    public void setModel(CmsModel cmsModel) {
        this.model = cmsModel;
    }

    public CmsChannel getParent() {
        return parent;
    }

    public CmsChannel parent(CmsChannel cmsChannel) {
        this.parent = cmsChannel;
        return this;
    }

    public void setParent(CmsChannel cmsChannel) {
        this.parent = cmsChannel;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        CmsChannel cmsChannel = (CmsChannel) o;
        if (cmsChannel.id == null || id == null) {
            return false;
        }
        return Objects.equals(id, cmsChannel.id);
    }

    @Override
    public int hashCode() {
        return Objects.hashCode(id);
    }

    @Override
    public String toString() {
        return "CmsChannel{" +
            "id=" + id +
            ", name='" + name + "'" +
            ", code='" + code + "'" +
            ", sort='" + sort + "'" +
            ", tplChannel='" + tplChannel + "'" +
            ", tplContent='" + tplContent + "'" +
            '}';
    }
}
